package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"user-profile/model/mysql"

	"user-profile/userProfileRpc/internal/svc"
	"user-profile/userProfileRpc/userProfileRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *userProfileRpc.UserUpdateRequest) (*userProfileRpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		HeadImage: in.HeadImage,
		Name:      in.Name,
		Tel:       in.Tel,
		Birthday:  in.Birthday,
		Sex:       in.Sex,
	}
	err := mysql.DB.Debug().Updates(&user).Error
	if err != nil {
		return nil, fmt.Errorf("用户信息完善失败")
	}
	return &userProfileRpc.UserUpdateResponse{
		Id: int64(user.ID),
	}, nil
}
